#!/usr/bin/env
__author__ = 'Gordon Sun'
import math
import sys
import time
from copy import deepcopy

def vol_sphere(radius):
  return (4.0/3.0)*math.pi*radius**3

def vol_sphere2radius(volume):
  return ((3.0/4.0)*volume*(1/math.pi))**(1.0/3.0)

def SA_sphere(radius):
  return 4*math.pi*radius**2

def SA2radius(SA):
  return math.sqrt(SA/(math.pi*4.0))

def earth_temp_alt(height):
  #https://www.grc.nasa.gov/www/k-12/airplane/atmosmet.html
  if height>25000:
    return -131.21+0.00299*height+273.15
  elif height>11000 and height<25000:
    return -56.46+273.15
  elif height<11000:
    return 15.04-(0.00649*height)+273.15

def earth_pressure_alt(height):
  #https://www.grc.nasa.gov/www/k-12/airplane/atmosmet.html
  if height>25000:
    return 2.488*(((141.89+0.00299*height)/216.6)**-11.388)
  elif height>11000 and height<25000:
    return 22.65*math.exp(1.73-(0.000157*height))
  elif height<11000:
    return 101.29*(((273.1-131.21+(0.00299*height))/288.08)**-5.256)
  else:
    return 0

def mars_temp_alt(height):
  #https://www.grc.nasa.gov/www/k-12/airplane/atmosmrm.html
  if height>7000:
    return -23.4-0.00222*height+273.15
  else:
    return -31-0.000998*height+273.15

def mars_pressure_alt(height):
  #https://www.grc.nasa.gov/www/k-12/airplane/atmosmet.html
  return 0.699*math.exp(-0.00009*height)

def m3_2_l(cubicmeters):
  return cubicmeters/1000.0

def vol2SA(vol):
  r=vol_sphere2radius(vol)
  return SA_sphere(r)


def main(args):
    print "This program is intended to calculate the characteristics of a balloon with a payload.  It takes in the \n" \
          "\t(1) payload weight\n" \
          "\t(2) mols of gas loaded into balloon\n" \
          "\t(3) Molar mass of gas loaded into balloon\n" \
          "\t(4) balloon material elasticity stretch percentage\n" \
          "\t(5) Earth or Mars\n"\
          "In return, it outputs the :" \
          "\t(1) balloon volume swell\n" \
          "\t(2) max attainable altitude\n" \
          "\t(3) pressure differential from start to finish altitude\n" \
          "\t(4) temperature change in altitude\n"\
          "The program assumes the balloon is massless, the mass of the payload accounts for any tethering weight,\n"\
          " the balloon is filled at STP, and released at sea level.\n\n"
    time.sleep(0)
    while True:
        payload_weight = raw_input("What is the balloon's payload weight in kg?\n")
        try:
            payload_weight = float(payload_weight)
            break
        except ValueError:
            print "Please enter a number.\n"
            continue
    while True:
        mol_vol = raw_input("What is the balloon's fill volume in mols of gas?\n")
        try:
            mol_vol = float(mol_vol)
            break
        except ValueError:
            print "Please enter a number.\n"
            continue
    while True:
        MM = raw_input("What is the molar mass of the gas loaded onto the balloon?\n")
        try:
            MM = float(MM)
            break
        except ValueError:
            print "Please enter a number.\n"
            continue
    while True:
        ESP = raw_input("What is the balloon's elastic limit stretch percentage?\n")
        try:
            ESP = float(ESP)
            break
        except ValueError:
            print "Please enter a number.\n"
            continue
    while True:
        planet = raw_input("Which planet? (m)ars or (e)arth?\n")
        try:
            if planet.lower()=="e" or planet.lower()=="m":
                break
        except False:
            print "Please enter either m or e.\n"
            continue
    R=0
    g=0
    if planet.lower()=="e":
      globe="earth"
      R=287.0
      g=9.81
    else:
      globe="mars"
      R=192.0
      g=3.711

    print "\n\nPlanet: "+ globe+"\n\n"\
          "Balloon Initial Mols: " + str(mol_vol) + " m^3\n" \
          "Balloon Payload weight: "+str(payload_weight)+ " kg\n"\
          "Balloon Elastic Limit: "+str(ESP)+" %\n"\
          "Gas Molar mass: "+str(MM)+" g/mol\n"


    if globe=="earth":
      V_i = mol_vol*R*earth_temp_alt(0)/earth_pressure_alt(0)
      V_i2 = deepcopy(V_i)
      V_f = V_i*(100+ESP)/100.0
      h=0
      while V_i2<V_f:
        V_i2 = mol_vol*R*earth_temp_alt(h)/earth_pressure_alt(h)
        h+=1
    else:
      V_i = mol_vol*R*mars_temp_alt(0)/mars_pressure_alt(0)
      V_i2 = deepcopy(V_i)
      V_f = V_i*(100+ESP)/100.0
      h=0
      while V_i2<V_f:
        V_i2 = mol_vol*R*mars_temp_alt(h)/mars_pressure_alt(h)
        h+=1
    print "Max Unburdened Balloon Altitude: " +str(h)+" m\n"

    if globe=="earth":
      h=0
      Vol_i = mol_vol*R*earth_temp_alt(h)/earth_pressure_alt(h)
      density_b = MM*mol_vol/Vol_i
      density_air = (MM*earth_pressure_alt(h))/(R*earth_temp_alt(h))

      F_net=((density_air-density_b)*g*Vol_i)-(payload_weight*g*h)
      while F_net>0:
        Vol = mol_vol*R*earth_temp_alt(h)/earth_pressure_alt(h)
        density_b = MM*mol_vol/Vol
        density_air = (MM*earth_pressure_alt(h))/(R*earth_temp_alt(h))
        F_net = -(payload_weight*g*h)+((density_air-density_b)*g*Vol)
        h+=1
    else:
      h=0
      Vol_i = mol_vol*R*mars_temp_alt(h)/mars_pressure_alt(h)
      density_b = MM*mol_vol/Vol_i
      density_air = (MM*mars_pressure_alt(h))/(R*mars_temp_alt(h))

      F_net=((density_air-density_b)*g*Vol_i)-(payload_weight*g*h)
      while F_net>0:
        Vol = mol_vol*R*mars_temp_alt(h)/mars_pressure_alt(h)
        density_b = MM*mol_vol/Vol
        density_air = (MM*mars_pressure_alt(h))/(R*mars_temp_alt(h))
        F_net = -(payload_weight*g*h)+((density_air-density_b)*g*Vol)
        h+=1

    if globe=="earth":
      Vol_f = mol_vol*R*earth_temp_alt(h)/earth_pressure_alt(h)
      T_i = earth_temp_alt(0)
      T_f = earth_temp_alt(h)
      P_i = earth_pressure_alt(0)
      P_f = earth_pressure_alt(h)
    else:
      Vol_f = mol_vol*R*mars_temp_alt(h)/mars_pressure_alt(h)
      T_i = mars_temp_alt(0)
      T_f = mars_temp_alt(h)
      P_i = mars_pressure_alt(0)
      P_f = mars_pressure_alt(h)
    print "Max Balloon Altitude with Payload: "+str(h)+" m\n"
    print "Balloon initial Volume: "+str(Vol_i)+" m^3\n"
    print  "Balloon final Volume: "+str(Vol_f)+" m^3\n"
    print "Balloon volume increase: "+str(Vol_f-Vol_i)+" m^3\n"
    print "Balloon surface area increase: "+str(vol2SA(Vol_f)-vol2SA(Vol_i))+" m^3\n"
    print "Pressure change in altitude: "+str(P_f-P_i)+ " kPA\n"
    print "Temperature change in altitude: "+str(T_f-T_i)+ " K\n"
    print "Initial Temperature: "+str(T_i)+ " K\n"
    print "Final Temperature: "+str(T_f)+ " K\n"
    print "Initial Pressure: "+str(P_i)+ " kPA\n"
    print "Final Pressure: "+str(P_f)+ " kPA\n"
    sys.exit()





if __name__ == '__main__':
    main(sys.argv)















